package notifiers;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import rsshunter.Encoder;

/**
 * MailSender is handling sending emails using Gmail account.
 */
public class MailSender {

    private Properties props;
    private String host;
    private String user;
    private String pass;
    private String to;

    /**
     * Creates new MailSender instance.
     *
     * @param host Email smtp address.
     * @param port Sending port.
     */
    public MailSender(String host, String port) {
        props = System.getProperties();
        this.host = host;
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", port);
        props.put("mail.smtp.starttls.enable", "true");
    }

    /**
     * Setting sender username and passord.
     *
     * @param user E-mail username.
     * @param encryptedPassword Email encrypted password.
     */
    public void setAuthentication(String user, String encryptedPassword) {
        this.user = user;
        try {
            this.pass = Encoder.decrypt(encryptedPassword);
        } catch (GeneralSecurityException ex) {
            System.err.println(
                    "Error decrypting password to MailSender: " + ex);
        } catch (IOException ex) {
            System.err.println(
                    "Error decrypting password to MailSender: " + ex);
        }
    }

    public boolean checkUserData(String username, String password) {
        try {
            Session session = Session.getDefaultInstance(props, null);
            Transport transport = session.getTransport("smtp");
            transport.connect(host, username, password);
            transport.close();
        } catch (MessagingException ex) {
            return false;
        }
        return true;
    }

    /**
     * Setting an adresee.
     *
     * @param to E-mail address.
     */
    public void setTo(String to) {
        this.to = to;
    }

    /**
     * Sends an email message.
     *
     * @param subject Subject of email.
     * @param content Content of email.
     */
    public void sendMail(String subject, String content)
            throws MessagingException {
        Session session = Session.getDefaultInstance(props, null);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(user));
        message.addRecipient(Message.RecipientType.TO,
                new InternetAddress(to));
        message.setSubject(subject);
        message.setText(content);

        Transport transport = session.getTransport("smtp");
        transport.connect(host, user, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
}
